package by.bucha.testproject.service;

import by.bucha.testproject.service.handlers.impl.DbDeleteHandler;
import by.bucha.testproject.service.handlers.impl.DbInsertHandler;
import by.bucha.testproject.service.handlers.impl.DbQueryHandler;
import by.bucha.testproject.service.handlers.impl.DbUpdateHandler;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.text.TextUtils;

public class ApplicationService extends Service {

	public static final String EXTRA_ON_ERROR = "ACTION_ON_ERROR";

	public static final String ACTION_DELETE = "delete";

	public static final String ACTION_INSERT = "insert";

	public static final String ACTION_UPDATE = "update";

	public static final String ACTION_QUERY = "query";

	public static final String EXTRA_CREATED_ROW_URL = "created row url after inserting to db";

	public static final String EXTRA_NUMBER_ROWS_UPDATED = "the number of rows updated";

	public static final String EXTRA_URI = "get_uri";

	public static final String EXTRA_COLUMNS = "columns";

	public static final String EXTRA_SORT_ORDER = "sort_order";

	public static final String EXTRA_SERIALIZED_MODEL = "string of serialized model";

	public static final String EXTRA_QUERY_RESPONSE = "response query";

	public static final String EXTRA_SELECTION = "selection";

	public static final String EXTRA_ROWS_NUMBER_DELETED = "The number of rows deleted";

	public static final String EXTRA_SELECTION_ARGS = "selection_args";

	public static final String EXTRA_STATUS_RECIVER = "result_receiver";

	public static final String EXTRA_MODEL_TYPE = "type of return/insert model";

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null) {
			String action = intent.getAction();
			if (!TextUtils.isEmpty(action)) {
				ResultReceiver callback = intent
						.getParcelableExtra(EXTRA_STATUS_RECIVER);
				if (action.equals(ACTION_DELETE)) {
					new DbDeleteHandler().execute(intent, this, callback);
				} else if (action.equals(ACTION_INSERT)) {
					new DbInsertHandler().execute(intent, this, callback);
				} else if (action.equals(ACTION_UPDATE)) {
					new DbUpdateHandler().execute(intent, this, callback);
				} else if (action.equals(ACTION_QUERY)) {
					new DbQueryHandler().execute(intent, this, callback);
				}
			}
		}

		return START_NOT_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}
